package com.cr.crandroid.ui.person.user;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cr.crandroid.R;
import com.cr.crandroid.base.BaseFragment2;
import com.cr.crandroid.bean.User;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.utils.OConvertUtils;
import com.cr.crandroid.utils.OkHttpManager;
import com.cr.crandroid.vo.CarVo;
import com.google.android.material.snackbar.Snackbar;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class RegisterFragment extends BaseFragment2 {

    private EditText editText;
    private EditText editText2;
    private EditText editText3;
    private EditText editText4;
    private EditText editText5;
    private RadioGroup radioGroup;
    private User user = new User();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_register, container, false);
        editText = root.findViewById(R.id.editText);
        editText2 = root.findViewById(R.id.editText2);
        editText3 = root.findViewById(R.id.editText3);
        editText4 = root.findViewById(R.id.editText4);
        editText5 = root.findViewById(R.id.editText5);
        radioGroup = root.findViewById(R.id.radioGroup);
        Button button = root.findViewById(R.id.button);
        button.setOnClickListener(this::signUp);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = radioGroup.findViewById(i);
                if (radioButton.getText().toString().equals("男")) {
                    user.setSex("1");
                } else {
                    user.setSex("2");
                }
            }
        });
        editText.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Map<String, String> check = new HashMap<>();
                            check.put("username", editText.getText().toString());
                            OkHttpManager.get(CommonConstant.URL + "/user/checkUsername", check, new Callback() {
                                @Override
                                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                    String res = response.body().string();
                                    JsonObject result = JsonParser.parseString(res).getAsJsonObject();
                                    if (Integer.parseInt(String.valueOf(result.get("code"))) != CommonConstant.ERROR) {
                                        if (Boolean.parseBoolean(result.get("result").toString())) {
                                            getActivity().runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    editText.setError("该账号已注册");
                                                }
                                            });
                                        }

                                    }
                                }

                                @Override
                                public void onFailure(@NonNull Call call, @NonNull IOException e) {

                                }
                            });
                        }
                    }).start();
                }
            }
        });
        editText3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!editable.toString().equals(editText2.getText().toString())) {
                    editText3.setError("两次密码不一致");
                }
            }
        });
        return root;
    }

    private void signUp(View view) {
        String name = editText.getText().toString();
        String password = editText2.getText().toString();
        String rePassword = editText3.getText().toString();
        String realName = editText4.getText().toString();
        String phone = editText5.getText().toString();

        if (TextUtils.isEmpty(name)) {
            editText.setError("账号不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            editText2.setError("密码不能为空");
            return;
        }
        if (!rePassword.equals(password)) {
            editText3.setError("两次密码不一致");
            return;
        }
        if (TextUtils.isEmpty(realName)) {
            editText.setError("姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            editText2.setError("手机号不能为空");
            return;
        }
        user.setUsername(name);
        user.setPassword(password);
        user.setPhone(phone);
        user.setRealName(realName);
        user.setRole("common");
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpManager.postJ(CommonConstant.URL + "/user/add", new Gson().toJson(user), new Callback() {
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String res = response.body().string();
                        JsonObject result = JsonParser.parseString(res).getAsJsonObject();
                        if (Integer.parseInt(String.valueOf(result.get("code"))) != CommonConstant.ERROR) {
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Snackbar.make(view, OConvertUtils.getString(result.get("message").toString()),
                                            Snackbar.LENGTH_LONG).show();
                                    Navigation.findNavController(view).popBackStack();

                                }
                            });
                        } else {
                            Snackbar.make(view, OConvertUtils.getString(result.get("message").toString()),
                                    Snackbar.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }
                });
            }
        }).start();
    }
}
